<?php $user= $session->read('User');
	$isedit=$action->checkPermissions($user,"confirm");
		 ?>
<?php if(!$isedit){ ?>
<style>
.code_list ul li{
	font-size:14px;
	font-weight:bold;line-height:24px;
}
.code_list ul li ul li{
	font-size:13px;
	color:#666666;
	font-weight:normal;line-height:20px;
}
</style>
<?php }?>
<div class="right_side_operate_area">
	<div class="right_side_title">
		
    	<?php if($isedit){ ?>
		<div class="right item_add_btn"><?php echo $html->link('添加', 'addcodetypes');?></div>
		<?php }else{
				echo '<div class="right enableaction">';
				echo "<a>添加</a>";
				echo '</div>';
			}
		 ?>
		<div class="clear"></div>
	</div>
</div>
<div class="right_side_scollbar">
	<div class="detail_info_panel">
		<div class="code_list">
			<ul>
				<?php 
					$dan="'";
					foreach($codetypes as $codetype)
					{
						echo "<li>";
						$typeid=$codetype["Codetype"]["id"];
						$txt_type_id="txt".$typeid;
						$ctype=$codetype["Codetype"]["type"];
						if($isedit){
							echo $form->text($txt_type_id,
								array("value"=>$ctype,"id"=>$txt_type_id,"class"=>"txtreadonly typew",
								"onclick"=>"edittxt(this)","onkeypress"=>"TextValidate()","maxlength"=>"20",
								"onblur"=>"changetptxt(this,'".$typeid."','".$ctype."')"));
							echo '<span class="code_delete" onclick="delctype(this,'.$dan.$typeid.$dan.')"></span>';
						}else{
							echo $ctype;
						}
						echo "<div class='clear'></div>";
						echo '<ul>';
						$value_copy="";
						foreach($codetype["Codevalue"] as $codevalue)
						{
								echo "<li>";
								$cvid=$codevalue["id"];
								$sort=$codevalue["sort"];
								$cvalue=$codevalue["value"];
								if($isedit){
									$txtid="txt".$cvid;
									echo $form->text($txtid,
									array("value"=>$cvalue,"id"=>$txtid,"class"=>"txtreadonly",
									"onclick"=>"edittxt(this)","onkeypress"=>"TextValidate()","maxlength"=>"20",
									"onblur"=>"changetxt(this,'".$cvid."','".$cvalue."')"));
									echo '<span class="code_delete" onclick="delcvalue(this,'.$dan.$cvid.$dan.')"></span>';
									echo '<span class="code_up" onclick="up(this,'.$dan.$typeid.$dan.','.$dan.$cvid.$dan.')"></span>';
									echo '<span class="code_down" onclick="down(this,'.$dan.$typeid.$dan.','.$dan.$cvid.$dan.')"></span>';
								}
								else{
									echo $cvalue;
								}
								echo "<div class='clear'></div></li>";
						}
						if($isedit){
							echo '<li><div class="add_code_line"><a href="javascript:void(0);" class="add_code" onclick="addcodetxt(this,'.$dan.$typeid.$dan.')">添加类型</a></div></li>';
						}
						echo '</ul>';
						echo "</li>";
					}
				?>
			</ul>
		</div>
	</div>
</div>
<?php echo $this->element('confirm',array("title"=>"删除提示","message"=>"你确定要删除吗？")) ?>
<script>
var webpath=$("#getpath").attr('href');
function edittxt(obj)
{
	$(obj).attr("class","");
}

function changetxt(obj,id,cvalue)
{
	$(obj).attr("class","txtreadonly");
	var codevalue=$(obj).val();
	if(codevalue.trim()==""){
		showmessage("请输入类型","F");
	}
	else if(cvalue!=codevalue)
	{
		 $.ajax({     	   
	        url: webpath+'types/editcodevalue/'+id+'/'+encodeURIComponent(codevalue), 
	        success:function(data){
	        	showmessage("保存成功");
	        }      
	     });
	}
	else{
		
	}
}

function changetptxt(obj,id,ctype)
{
	$(obj).attr("class","txtreadonly");
	var codetype=$(obj).val();
	if(codetype.trim()==""){
		showmessage("请输入类型","F");
	}
	else if(ctype!=codetype)
	{
		$.ajax({
			url:webpath+'types/editcodetype/'+id+'/'+encodeURIComponent(codetype),
			success:function(data)
			{
				showmessage("保存成功");
			}
		});
	}
}
function delctype(obj,id) {
	$(".confirm_model").dialog( "open" );
	$("#ok").click(function() {
		delctypes(obj,id);
		$(".confirm_model").dialog( "close" );
	});
	return false;
}
function delctypes(obj,id)
{
	$(obj).parent().remove();
	$.ajax({     	   
	        url: webpath+'types/delcodetype/'+id, 
	        success:function(data){
	        	showmessage("删除成功");
	        }      
	     });
}
function delcvalue(obj,id) {
	$(".confirm_model").dialog( "open" );
	$("#ok").click(function() {
		delcvalues(obj,id);
		$(".confirm_model").dialog( "close" );
	});
	return false;
}
function delcvalues(obj,id)
{
	$(obj).parent().remove();
	$.ajax({     	   
	        url: webpath+'types/delcodevalue/'+id, 
	        success:function(data){
	        	showmessage("删除成功");
	        }      
	     });
}
function addcodetxt(obj,typeid)
{
	var dan='"';
	$(obj).parent().parent().before("<li><div class='add_code_text'><input type='text' onkeypress='TextValidate()' maxlength='20' onchange='addcodevalue(this,"+dan+typeid+dan+")' /></div><span class='code_delete' onclick='delli(this)'></span><div class='clear'></div></li>");
}
function delli(obj)
{
	$(obj).parent().remove();
}
function addcodevalue(obj,typeid)
{
	var sort=$(obj).parent().parent().parent().find('.txtreadonly').length;
	if(obj.value.trim()==""){
		showmessage("请输入类型","F");
	}
	else if(obj.value!=""){
		$.ajax({     	   
		        url: webpath+'types/addcodevalue/'+typeid+'/'+encodeURIComponent(obj.value), 
		        success:function(data){
		        	$(obj).parent().parent().html(data);
		        	showmessage("保存成功");
		        }      
		     });
	}
}

function up(obj,typeid,id)
{  	
	var isset=$(obj).parent().prev().html();	
	if(isset==null){
		showmessage("已经是第一个","F");
	}else{
		var isf=$(obj).parent().prev().find('.add_code_text').length;
		$(obj).parent().prev().before($(obj).parent());	 
		showmessage("设置成功");
		if(isf==0){
			dosort("up",obj,typeid,id);
		}
	}
}  
function down(obj,typeid,id)
{  
	var isset=$(obj).parent().next().find('.add_code_line').length;
	if(isset==0){
	    $(obj).parent().next().after($(obj).parent());  
	    showmessage("设置成功");
	    var isf=$(obj).parent().next().find('.add_code_text').length;
	    if(isf==0){
	    	dosort("down",obj,typeid,id);
	    }
    }else{
    	showmessage("已经是最后一个","F");
    }
}  
function dosort(action,obj,typeid,id)
{
	var dourl=webpath+'types/'+action+'/'+typeid+'/'+id;
	$.ajax({     	   
	        url: dourl, 
	        success:function(data){
	        	
	        }      
	     });
}
</script>
